package com.breakfast.fun.adapter.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.data.entity.user.ShopData;
import com.breakfast.fun.data.entity.user.order.OrderData;
import com.breakfast.fun.event.PayEvent;
import com.breakfast.fun.ui.home.comment.CommentActivity;
import com.breakfast.fun.ui.user.order.BFOrderDetailActivity;
import com.breakfast.fun.util.JumpUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.TimeUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/30.
 */
public class UserBFOrderAdapter extends BaseListAdapter<OrderData> {
    public UserBFOrderAdapter(Context context, List<OrderData> list) {
        super(context, list);
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_bforder, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        OrderData data = getItem(i);

        holder.tvItemBforderTime.setText(data.getOrderTime().substring(0, data.getOrderTime().length() - 3));
        holder.tvItemBforderCount.setText("" + data.getQuantity());
        holder.tvItemBforderPrice.setText("￥" + String.format("%.2f", data.getTotalPrice()) + "元");

        if (data.getState() == 0) {
            //待支付
            holder.tvItemBforderState.setText("待付款");
            holder.tvItemBforderAction.setText("去付款");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#D90504"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_red);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                EventBus.getDefault().post(new PayEvent(data.getOrderNo(), "BFOrderFragment"));
            });

        } else if (data.getState() == 1) {
            //已支付
            holder.tvItemBforderState.setText("未评价");
            holder.tvItemBforderAction.setText("去评价");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#F19601"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_orange);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, CommentActivity.class);
                intent.putExtra("orderId", data.getId() + "");
                intent.putExtra("orderNo", data.getOrderNo());
                intent.putExtra("orderTime", data.getOrderTime());
                mContext.startActivity(intent);
            });
        } else if (data.getState() == 2) {
            //已评价
            holder.tvItemBforderState.setText("交易成功");
            holder.tvItemBforderAction.setText("再来一单");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#F19601"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_orange);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                JumpUtil.toHome(mContext);
            });
        } else if (data.getState() == 3) {
            //已取消
            if (data.getPayState() == 3) {
                holder.tvItemBforderState.setText("退款中");
            } else if (data.getPayState() == 4) {
                holder.tvItemBforderState.setText("已退款");
            } else {
                holder.tvItemBforderState.setText("已取消");
            }
            holder.tvItemBforderAction.setText("再来一单");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#F19601"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_orange);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                JumpUtil.toHome(mContext);
            });
        }


        Map<String, List<ShopData>> dayShopDatas = new HashMap<>();

        View view = null;
        Map<String, List<ShopData>> shopDatas = data.getShopData();
        holder.llItemBforderList.removeAllViews();
        for (Map.Entry<String, List<ShopData>> entry : shopDatas.entrySet()) {
            String sendDate = "";
            for (ShopData shopData : entry.getValue()) {
                sendDate = shopData.getSendDate();
                List<ShopData> shopDatas1 = new ArrayList<>();
                if (dayShopDatas.containsKey(sendDate)) {
                    shopDatas1 = dayShopDatas.get(sendDate);
                    shopDatas1.add(shopData);
                    dayShopDatas.put(sendDate, shopDatas1);
                } else {
                    shopDatas1.add(shopData);
                    dayShopDatas.put(sendDate, shopDatas1);
                }
            }
        }

        try {
            Object[] key_arr = dayShopDatas.keySet().toArray();
            Arrays.sort(key_arr);
            for (Object key : key_arr) {
                List<ShopData> value = dayShopDatas.get(key);
                view = mInflater.inflate(R.layout.item_bforder, null);
                TextView tvItemBforderName = (TextView) view.findViewById(R.id.tv_item_bforder_name);
                TextView tvItemBforderSendDate = (TextView) view.findViewById(R.id.tv_item_bforder_send_date);
                ImageView ivItemBforder = (ImageView) view.findViewById(R.id.iv_item_bforder);

                tvItemBforderName.setText(value.get(0).getProductName() + " 等" + value.size() + "类商品");
                tvItemBforderSendDate.setText("送餐日期:" + key.toString() + " [" + TimeUtil.CalcuDate(key.toString()) + "]");
                imageUtil.loadUrlImage(value.get(0).getProductImage(), ivItemBforder);
                holder.llItemBforderList.addView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

       /* for(Map.Entry<String,List<ShopData>> entry : dayShopDatas.entrySet()){
            view = mInflater.inflate(R.layout.item_bforder,null);
            TextView tvItemBforderName = (TextView) view.findViewById(R.id.tv_item_bforder_name);
            TextView tvItemBforderSendDate = (TextView) view.findViewById(R.id.tv_item_bforder_send_date);
            ImageView ivItemBforder = (ImageView) view.findViewById(R.id.iv_item_bforder);

            tvItemBforderName.setText(entry.getValue().get(0).getProductName() + " 等" + entry.getValue().size() + "类商品");
            tvItemBforderSendDate.setText("送餐日期:"+entry.getKey() + " ["  + TimeUtil.CalcuDate(entry.getKey()) + "]");
            imageUtil.loadUrlImage(entry.getValue().get(0).getProductImage(),ivItemBforder);
            holder.llItemBforderList.addView(view);
        }*/

        holder.llItemBforderList.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, BFOrderDetailActivity.class);
            intent.putExtra("orderId", data.getId() + "");
            intent.putExtra("orderNo", data.getOrderNo());
            mContext.startActivity(intent);
        });

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tv_item_bforder_tip)
        TextView tvItemBforderTip;
        @Bind(R.id.tv_item_bforder_time)
        TextView tvItemBforderTime;
        @Bind(R.id.tv_item_bforder_state)
        TextView tvItemBforderState;
        @Bind(R.id.ll_item_bforder_list)
        LinearLayout llItemBforderList;
        @Bind(R.id.tv_item_bforder_count)
        TextView tvItemBforderCount;
        @Bind(R.id.tv_item_bforder_price)
        TextView tvItemBforderPrice;
        @Bind(R.id.tv_item_bforder_action)
        TextView tvItemBforderAction;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
